package com.example.springvue.common.handler;

import com.example.springvue.model.AdminUser;
import com.example.springvue.model.AdminUserHeader;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Optional;

/***
 * ThreadLocal记录后台用户信息在线程内
 * */
public class AdminUserHeaderHodler {

        private static ThreadLocal<AdminUser> AdminUserThread = new ThreadLocal<>();


        public static void set(AdminUser adminUserHeader) {
            AdminUserThread.set(adminUserHeader);
        }

        public static AdminUser get() {
            return AdminUserThread.get();
        }

        /**
         * 获取当前登录用户的ID
         * 未登录返回null
         *
         * @return
         */
        public static int getUserId() {
            AdminUser user = get();
            if (user != null) {
                return user.getId();
            }
            return 0;
        }

        //防止内存泄漏
        public static void remove() {
            AdminUserThread.remove();
        }

}
